//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package top.ezzz.ai;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import org.noear.solon.Utils;
import org.noear.solon.ai.chat.ChatSession;
import org.noear.solon.ai.chat.message.ChatMessage;


public class ChatSessionLimit implements ChatSession {

    private final String sessionId;
    private List<ChatMessage> messages = new ArrayList<>(10);
    private int limit = 10;

    public ChatSessionLimit() {
        this.sessionId = Utils.uuid();
    }

    public ChatSessionLimit(String sessionId) {
        this.sessionId = sessionId;
    }

    public ChatSessionLimit(int limit) {
        this.sessionId = Utils.uuid();
        this.limit = limit;
    }

    public ChatSessionLimit(String sessionId, int limit) {
        this.sessionId = sessionId;
        this.limit = limit;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public void setMessages(List<ChatMessage> messages) {
        this.messages = messages;
    }

    /**
     * 获取会话id
     */
    @Override
    public String getSessionId() {
        return sessionId;
    }

    /**
     * 获取所有消息
     */
    @Override
    public List<ChatMessage> getMessages() {
        return messages;
    }

    /**
     * 添加消息
     */
    @Override
    public void addMessage(ChatMessage... messages) {
        if (messages.length == 1) {
            this.messages.add(messages[0]);
        } else {
            Collections.addAll(this.messages, messages);
        }
        int size = this.messages.size();
        if (limit < size) {
            this.messages = CollUtil.sub(this.messages, size - limit, size);
        }

    }

    /**
     * 清空消息
     */
    @Override
    public void clear() {
        this.messages.clear();
    }
}
